package com.cr.crandroid.ui.home.rent;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.cr.crandroid.R;
import com.cr.crandroid.bean.Rent;
import com.cr.crandroid.ui.person.user.LoginViewModel;
import com.cr.crandroid.utils.OConvertUtils;
import com.cr.crandroid.vo.CarVo;
import com.cr.crandroid.vo.RentVo;
import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class AddRentFragment extends Fragment {

    private EditText editText;
    private EditText editText2;
    private EditText editText3;
    private Button button;
    private RentVo rent = new RentVo();
    private CarVo car;
    private CalendarPicker calendarPicker;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState, FragmentActivity fragmentActivity) {
        View root = inflater.inflate(R.layout.add_rent, container, false);
        editText = root.findViewById(R.id.editText);
        editText2 = root.findViewById(R.id.editText2);
        editText3 = root.findViewById(R.id.editText3);
        button = root.findViewById(R.id.button);

        calendarPicker = new CalendarPicker(fragmentActivity);
        Bundle arguments = savedInstanceState;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (arguments != null && arguments.containsKey("carVo")) {
            String carJson = arguments.getString("carVo");
            car = new Gson().fromJson(carJson, new TypeToken<CarVo>() {
            }.getType());

        }
        if (LoginViewModel.user.has("id")) {
            editText.setText(OConvertUtils.getString(LoginViewModel.user.get("realName").toString()));
            editText2.setText(OConvertUtils.getString(LoginViewModel.user.get("phone").toString()));
        }
        button.setOnClickListener(view -> {
            try {
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(new Date());
                calendar.add(calendar.DATE, 3);
                Date rangeEnd = calendar.getTime();
                if (rangeEnd.getTime() < sdf.parse(car.getRentalEnd()).getTime())
                    calendarPicker.setRangeDate(new Date(), rangeEnd);
                else calendarPicker.setRangeDate(new Date(), sdf.parse(car.getRentalEnd()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            calendarPicker.show();
        });
        calendarPicker.setOnRangeDatePickListener(new OnRangeDatePickListener() {
            @Override
            public void onRangeDatePicked(@NonNull Date startDate, @NonNull Date endDate) {
                rent.setStartTime(sdf.format(startDate));
                rent.setEndTime(sdf.format(endDate));
                editText3.setText(sdf.format(startDate) + "至" + sdf.format(endDate));
            }
        });
        return root;
    }

    public boolean judge() {
        String realName = editText.getText().toString();
        String phone = editText2.getText().toString();
        String rentDate = editText3.getText().toString();

        if (TextUtils.isEmpty(realName)) {
            editText.setError("联系姓名不能为空");
            return false;
        }
        if (TextUtils.isEmpty(phone)) {
            editText2.setError("联系手机号不能为空");
            return false;
        }
        if (TextUtils.isEmpty(rentDate)) {
            editText3.setError("预约日期不能为空");
            return false;
        }
        rent.setContact(realName);
        rent.setContactPhone(phone);
        return true;
    }

    public RentVo getRent() {
        return rent;
    }
}
